#pragma once
#include <QtGui>

namespace XpsAssets
{

template <class T>
class Manager
{

private:
	int	id;
	QMap<int,T*>	data;

public:

	Manager(void)
	{
		id = 0;
	}

	~Manager(void) {};


	void reset()
	{
		//qDeleteAll(m_data);
		data.clear();
		id = 0;
	}

	void deleteContent()
	{
		qDeleteAll(data);
		/*while (data.begin() != data.end())
		{
			delete *data.begin();
			++data.begin();
		}*/
 		reset();
	}

	int	count()
	{
		return data.count();
	}

	int	size()
	{
		return data.size();
	}

	int	lastid()
	{
		return id;
	}
	
	T* create()
	{
		T* rslt = new T();
		id++;
		rslt->setID(id);
		data.insert(id, rslt);

		return rslt;
	}

	/// create with id parameter
	/// should only be used to load assets from
	/// file with existing id's
	T* create(int id)
	{
		if (data.keys().contains(id)) 
		{
			qDebug() << "Manager (error): Tried to create asset with allready registered id.";
			return NULL;
		}

		T* rslt = new T();
		rslt->setID(id);
		data.insert(id, rslt);

		if (id > this->id) this->id = id;
		return rslt;
	}


	T* get(int id)
	{
		return data[id];
	}

	void remove(int id)
	{
		T* tbr = data.take(id);
		delete tbr;
		tbr = NULL;
	}

	T* last()
	{
		return data[data.keys().last()];
	}

	T* at(int pos)
	{
		return data[data.keys().at(pos)];
	}

	int pos(int id)
	{
		return data.keys().indexOf(id);
	}

	void copyFrom(Manager<T> &m)
	{
		reset();
		for (int i = 0; i < m.count(); i++)
		{
			data.insert(m.at(i)->id(), m.at(i));
		}
		id = m.lastid();
	}
};

}
